import { Logger } from "./Logger";
import { EventManager } from "./EventManager";
import GameController from "../GameController";
import { NativeSdkHelper } from "../sdk/NativeSdkHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ComponentBase extends cc.Component {

    //是否已经初始化节点
    public isInit: boolean = false;

    

    onLoad() {
        // if (!NativeSdkHelper.isIos()) {
            this.node.getComponents(cc.Label).forEach((txt: cc.Label) => {
                if (txt.useSystemFont) {
                    txt.fontFamily = "Microsoft YaHei"
                }
                txt.cacheMode = cc.Label.CacheMode.BITMAP;
            });
            this.node.children.forEach(childNode => {
                childNode.getComponents(cc.Label).forEach((txt: cc.Label) => {
                    if (txt.useSystemFont) {
                        txt.fontFamily = "Microsoft YaHei"
                    }
                    txt.cacheMode = cc.Label.CacheMode.BITMAP;
                });
            });
        // } else {
        //     this.node.getComponents(cc.Label).forEach((txt: cc.Label) => {
        //         txt.cacheMode = cc.Label.CacheMode.BITMAP;
        //     });
        //     this.node.children.forEach(childNode => {
        //         childNode.getComponents(cc.Label).forEach((txt: cc.Label) => {
        //             txt.cacheMode = cc.Label.CacheMode.BITMAP;
        //         });
        //     });
        // }
    }

    private checkAddClickEvent(node: cc.Node) {
        node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
            GameController.instance.onCLickEvent(event)
        }, node);
        node.children.forEach(childNode => {
            this.checkAddClickEvent(childNode)
        });
    }

    start() {

    }

    onEnable() {

    }

    onDisable() {
        this._dispose();
    }


    /**获得子节点 */
    public findChild(parent: cc.Node, path: string): cc.Node {
        let paths: Array<string> = path.split("/");
        let child: cc.Node = parent;
        if (paths) {
            for (let i = 0; i < paths.length; ++i) {
                child = parent.getChildByName(paths[i]);
                parent = child;
                if (!parent) {
                    // Logger.error(`findChild ----------- ${path}  parent is null`);
                    return;
                }
            }
        }
        return child;
    }
    /** 获取子节点 或者 子节点组件 */
    public getChild(parent: cc.Node, childName: string, childType: any = cc.Node): any {
        if (!parent) {
            // Logger.error(`getChild ----------- ${childName}  parent is null`);
            return;
        }
        if (childType == cc.Node) {
            return this.findChild(parent, childName);
        }
        let childNode: cc.Node = this.findChild(parent, childName)
        if (childNode) {
            return childNode.getComponent(childType);
        }
    }


    public hideAllChildren(node: cc.Node) {
        for (let i = 0; i < node.childrenCount; i++) {
            node.children[i].active = false;
        }
    }

    public getToggleContainerIndex(toggleContainer: cc.ToggleContainer) {
        for (let i = 0; i < toggleContainer.toggleItems.length; i++) {
            let toggle: cc.Toggle = toggleContainer.toggleItems[i]
            if (toggle.isChecked) {
                return i;
            }
        }
        return 0
    }

    public setToggleContainerIndex(toggleContainer: cc.ToggleContainer, index: number) {
        toggleContainer.toggleItems[toggleContainer.toggleItems.length - 1].isChecked = true
        for (let i = 0; i < toggleContainer.toggleItems.length; i++) {
            let toggle: cc.Toggle = toggleContainer.toggleItems[i]
            if (i == index) {
                toggle.isChecked = true;
            }
        }
    }


    private _dispose() {
        this.node.stopAllActions();
        this.unscheduleAllCallbacks();
        // Logger.log("_dispose==", this.name)
        EventManager.instance.clear(this);
    }

}
